package edu.qd.learn.newchapter.chapter02.exam02;

/**
 * 词条表
 * 管理的是大量的词条
 *
 * @author 布衣
 */
public class WordReportTable {
    //头节点
    private ReportNode head;
    //尾节点
    private ReportNode tail;

    public void add(WordReport ones) {
        if (head == null) {
            head = new ReportNode(ones);
            tail = head;
            return;
        }
        //新节点
        ReportNode newNode = new ReportNode(ones);
        //尾节点的next指向新节点
        tail.next = newNode;
        newNode.prev = tail;
        //移动尾节点
        tail = tail.next;
    }

    public ReportNode getHead() {
        return head;
    }

    /**
     * 词条的条目节点
     */
    public class ReportNode {
        //前一个节点
        ReportNode prev;
        //当前节点的词条
        WordReport reports;
        //后一个节点
        ReportNode next;

        public ReportNode(WordReport reports) {
            this.reports = reports;
        }
    }

    @Override
    public String toString() {
        String result = "";
        ReportNode temp = head;
        while (temp != null) {
            result += temp.reports;
            temp = temp.next;
        }
        return result;
    }
}
